﻿MPT.addAction("clm_social", function(_elm) {

    var shift_l_jQ = $('.e_shift_l', _elm);
    var shift_r_jQ = $('.e_shift_r', _elm);
    var more_jQ = $('.e_more', _elm);
    var body_jQ = $('.e_body', _elm);
    var count_jQ = $('.e_count', _elm);
    var data_jQ = $('.e_data', _elm);
    var no_data_jQ = $('.e_no_data', _elm);
    var friend_jQ = $('.e_friend', _elm);
    var follow_jQ = $('.e_follow', _elm);
    var fan_jQ = $('.e_fan', _elm);
    var title_jQ = $('.e_title', _elm);
    var tel_jQ = $('.e_tel', _elm);
    var sub_nav_jQ = $('.e_sub_nav', _elm);

    var userID = Util.GetHashParam();
    var socials = [];
    if(userID){
        now_ID = userID;

        tel_jQ.html('TA');
        sub_nav_jQ.hide();

        socials[1] = { "no_data_hint": "还没有好友，请先添加好友", "title": "好友", "more_url": MPT.Config["hash"]["u_friend"]+userID };
        socials[2] = { "no_data_hint": "还没有收听，请先添加关注", "title": "关注", "more_url": MPT.Config["hash"]["u_follow"]+userID };
        socials[3] = { "no_data_hint": "还没有粉丝", "title": "粉丝", "more_url": MPT.Config["hash"]["u_fan"]+userID };
    }else{
        now_ID = MPT.Config["session"]["userid"];
        socials[1] = { "no_data_hint": "还没有好友，请先添加好友", "title": "好友", "more_url": MPT.Config["hash"]["friend"] };
        socials[2] = { "no_data_hint": "还没有收听，请先添加关注", "title": "关注", "more_url": MPT.Config["hash"]["follow"] };
        socials[3] = { "no_data_hint": "还没有粉丝", "title": "粉丝", "more_url": MPT.Config["hash"]["fan"] };
    }

    var displayOneHead = function(_r) {
        var html = [];
        var userId = _r.uid;
        var nickName = _r.nickname;
        var truncName = Util.truncName(_r.nickname, 4);
        var avatarPath = Util.avatarPath(_r.uid);
        var spacePath = Util.spacePath(_r.uid);

        html.push('<li>');
        html.push('<h2 class="face"><a href=' + spacePath + ' title="' + nickName + '">');
        html.push('<img src="' + avatarPath + '" alt="' + nickName + '"></a></h2>');
        html.push('<p class="name" title="' + nickName + '">' + truncName + '</p>');
        html.push('</li>');
        return html.join('');
    }

    var displayEmptyHead = function() {
        return "<li>&nbsp;</li>";
    }

    var displaySocialList = function(_type) {

        title_jQ.html(socials[_type]["title"]);
        more_jQ.click(function() { window.location.hash = socials[_type]["more_url"] });
        data = { "pageNo": 1, "pageSize": 24, "type": _type ,"targetId": now_ID };

        Backend.findPersonForSpace(data, function(_resp) {
            if (Backend.pageBeanValidate(_resp)) {

                var results = _resp.results;
                var count = _resp.totalRecoreds;
                var html = [];

                count_jQ.html(count);
                no_data_jQ.hide();
                data_jQ.show();

                if (count <= 4) {
                    $.each(results, function(_i, _r) {
                        html.push(displayOneHead(_r));
                    });
                    data_jQ.height(85);
                } else {

                    var html_11 = [], html_12 = [], html_21 = [], html_22 = [], html_31 = [], html_32 = [];

                    for (var i = 0; i < 24; i++) {
                        if (i < 4) { html = html_11; }
                        else if (i < 8) { html = html_12; }
                        else if (i < 12) { html = html_21; }
                        else if (i < 16) { html = html_22; }
                        else if (i < 20) { html = html_31; }
                        else { html = html_32; }

                        if (i < count) {
                            html.push(displayOneHead(results[i]));
                        } else {
                            html.push(displayEmptyHead());
                        }
                    }

                    html = html_11.concat(html_21, html_31, html_12, html_22, html_32);
                    data_jQ.height(170);
                }

                body_jQ.html(html.join(''));

                var page_count = Math.ceil(results.length / 8);
                (new Paginate).createPageShift(shift_r_jQ, shift_l_jQ, body_jQ, 260, page_count, "fast");

            } else {
                no_data_jQ.html(socials[_type]["no_data_hint"]).show();
                data_jQ.hide();
                count_jQ.html('0');
            }
        });
    }

    var initialize = function() {

        friend_jQ.click(function() { displaySocialList(1); });
        follow_jQ.click(function() { displaySocialList(2); });
        fan_jQ.click(function() { displaySocialList(3); });

        friend_jQ.click();

    };

    initialize();
});